/*
 * 	<pre>
 *	EasyPeas.
 *  Copyright (C) 2007 S Owen
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Jul 24, 2007
 * Math.java
 * </pre>
 */
package org.testing;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.easypeas.annotations.Action;
import org.easypeas.annotations.EasyPea;
import org.easypeas.annotations.View;

@EasyPea(name = "math")
public class Math {
	@SuppressWarnings("unused")
	private static Logger logger = Logger.getLogger(Math.class);

	@View(name = "add")
	public void add(HttpServletRequest req, HttpServletResponse resp,
			Map<String, Object> map) {

	}
	
	@View(name = "add_widget")
	public void add_widget(HttpServletRequest req, HttpServletResponse resp,
			Map<String, Object> map) {

	}
        
        @View(name = "3x3",template = "/math/3times3.vm")
        public void threetimesthree(HttpServletRequest req, HttpServletResponse resp,
			Map<String, Object> map) {
            map.put("nine", 3 * 3);
        }
        
        @View(name = "tentimesten")
        public void tentimesten(HttpServletRequest req, HttpServletResponse resp,
			Map<String, Object> map) {
            map.put("hundred",10 * 10);
        }

	public Object id(String key) {
		return new Square();
	}

	@Action(name = "add", forwards = { "success=/math/add_result",
			"failure=/error.vm" })
	public String doAdd(HttpServletRequest req, HttpServletResponse res,
			Map<String, Object> map) throws Exception {
		try {
			String a = (String) req.getParameter("A");
			String b = (String) req.getParameter("B");
			int r = Integer.valueOf(a).intValue() + Integer.valueOf(b);
			map.put("result", r);
			return "success";
		} catch (Exception e) {
			map.put("error", e);
			e.printStackTrace();
			return "failure";
		}
	}

	@View(name = "add_result")
	public void addResult(HttpServletRequest req, HttpServletResponse resp,
			Map<String, Object> map) {

	}

	@EasyPea(name = "square")
	static public class Square {

		@View(name = "index", template = "/shape/index.vm")
		public void index(HttpServletRequest req, HttpServletResponse res,
				Map<String, Object> map) {
			map.put("shape", "square");
		}
	}
}
